home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / getpath.c_orig < prev    next >
Text File  |  1999-04-14  |  18KB  |  720 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Return the initial module search path. */
  33.  
  34. #include "Python.h"
  35. #include "osdefs.h"
  36.  
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <string.h>
  40.  
  41. #ifdef _AMIGA
  42. #include <proto/dos.h>
  43. #include "protos.h"
  44. #endif
  45.  
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif /* HAVE_UNISTD_H */
  49.  
  50. #include "protos/getpath_protos.h"
  51.  
  52. /* Search in some common locations for the associated Python libraries.
  53.  *
  54.  * Two directories must be found, the platform independent directory
  55.  * (prefix), containing the common .py and .pyc files, and the platform
  56.  * dependent directory (exec_prefix), containing the shared library
  57.  * modules.  Note that prefix and exec_prefix can be the same directory,
  58.  * but for some installations, they are different.
  59.  *
  60.  * Py_GetPath() carries out separate searches for prefix and exec_prefix.
  61.  * Each search tries a number of different locations until a ``landmark''
  62.  * file or directory is found.  If no prefix or exec_prefix is found, a
  63.  * warning message is issued and the preprocessor defined PREFIX and
  64.  * EXEC_PREFIX are used (even though they will not work); python carries on
  65.  * as best as is possible, but most imports will fail.
  66.  *
  67.  * Before any searches are done, the location of the executable is
  68.  * determined.  If argv[0] has one or more slashs in it, it is used
  69.  * unchanged.  Otherwise, it must have been invoked from the shell's path,
  70.  * so we search $PATH for the named executable and use that.  If the
  71.  * executable was not found on $PATH (or there was no $PATH environment
  72.  * variable), the original argv[0] string is used.
  73.  *
  74.  * Next, the executable location is examined to see if it is a symbolic
  75.  * link.  If so, the link is chased (correctly interpreting a relative
  76.  * pathname if one is found) and the directory of the link target is used.
  77.  *
  78.  * Finally, argv0_path is set to the directory containing the executable
  79.  * (i.e. the last component is stripped).
  80.  *
  81.  * With argv0_path in hand, we perform a number of steps.  The same steps
  82.  * are performed for prefix and for exec_prefix, but with a different
  83.  * landmark.
  84.  *
  85.  * Step 1. Are we running python out of the build directory?  This is
  86.  * checked by looking for a different kind of landmark relative to
  87.  * argv0_path.  For prefix, the landmark's path is derived from the VPATH
  88.  * preprocessor variable (taking into account that its value is almost, but
  89.  * not quite, what we need).  For exec_prefix, the landmark is
  90.  * Modules/Setup.  If the landmark is found, we're done.
  91.  *
  92.  * For the remaining steps, the prefix landmark will always be
  93.  * lib/python$VERSION/string.py and the exec_prefix will always be
  94.  * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
  95.  * number as supplied by the Makefile.  Note that this means that no more
  96.  * build directory checking is performed; if the first step did not find
  97.  * the landmarks, the assumption is that python is running from an
  98.  * installed setup.
  99.  *
  100.  * Step 2. See if the $PYTHONHOME environment variable points to the
  101.  * installed location of the Python libraries.  If $PYTHONHOME is set, then
  102.  * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
  103.  * directory, which is used for both, or the prefix and exec_prefix
  104.  * directories separated by a colon.
  105.  *
  106.  * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
  107.  * backtracking up the path until it is exhausted.  This is the most common
  108.  * step to succeed.  Note that if prefix and exec_prefix are different,
  109.  * exec_prefix is more likely to be found; however if exec_prefix is a
  110.  * subdirectory of prefix, both will be found.
  111.  *
  112.  * Step 4. Search the directories pointed to by the preprocessor variables
  113.  * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
  114.  * passed in as options to the configure script.
  115.  *
  116.  * That's it!
  117.  *
  118.  * Well, almost.  Once we have determined prefix and exec_prefix, the
  119.  * preprocesor variable PYTHONPATH is used to construct a path.  Each
  120.  * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
  121.  * containing the shared library modules is appended.  The environment
  122.  * variable $PYTHONPATH is inserted in front of it all.  Finally, the
  123.  * prefix and exec_prefix globals are tweaked so they reflect the values
  124.  * expected by other code, by stripping the "lib/python$VERSION/..." stuff
  125.  * off.  If either points to the build directory, the globals are reset to
  126.  * the corresponding preprocessor variables (so sys.prefix will reflect the
  127.  * installation location, even though sys.path points into the build
  128.  * directory).  This seems to make more sense given that currently the only
  129.  * known use of sys.prefix and sys.exec_prefix is for the ILU installation
  130.  * process to find the installed Python tree.
  131.  */
  132.  
  133. #ifndef VERSION
  134. #define VERSION "1.5"
  135. #endif
  136.  
  137. #ifndef VPATH
  138. #define VPATH "."
  139. #endif
  140.  
  141. #ifndef PREFIX
  142. #define PREFIX "/usr/local"
  143. #endif
  144.  
  145. #ifndef EXEC_PREFIX
  146. #define EXEC_PREFIX PREFIX
  147. #endif
  148.  
  149. #ifndef PYTHONPATH
  150. /* I know this isn't K&R C, but the Makefile specifies it anyway */
  151. #ifdef _AMIGA
  152. #define PYTHONPATH PREFIX "lib/python" VERSION ";" EXEC_PREFIX "lib/python" VERSION "/lib-dynload"
  153. #else /* !AMIGA */
  154. #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
  155.           EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
  156. #endif
  157. #endif
  158.  
  159. #ifndef LANDMARK
  160. #define LANDMARK "string.py"
  161. #endif
  162.  
  163. static char prefix[MAXPATHLEN+1];
  164. static char exec_prefix[MAXPATHLEN+1];
  165. static char progpath[MAXPATHLEN+1];
  166. static char *module_search_path = NULL;
  167. static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
  168.  
  169. #ifdef _AMIGA
  170. /* determine full program path */
  171. extern void Py_GetArgcArgv Py_PROTO((int *argc, char ***argv));    /* in main.c */
  172.  
  173. static const char *fullprogpath(void)
  174. {
  175.     static char path[MAXPATHLEN*2];
  176.     static char prog[MAXPATHLEN];
  177.  
  178.     BPTR dir;
  179.  
  180.     extern BOOL from_WB;    /* in main.c */
  181.  
  182.     // If the program name contains ':' or '/' it's not in the user's path
  183.     // and probably set by using Py_SetProgramName. In that case, just
  184.     // use this. If it exists!
  185.     strcpy(path,Py_GetProgramName());
  186.     if(strchr(path,':') || strchr(path,'/'))
  187.     {
  188.         if(!isxfile(path))
  189.         {
  190.             // Error; the specified file does not exist or is no exe
  191.             path[0]='\0';
  192.         }
  193.         return path;
  194.     }
  195.  
  196.     // Construct the full path of our executable program.
  197.     if(from_WB)
  198.     {
  199.         /* We're launced from WB, GetProgramName() won't work */
  200.         /* Use WB's argv[0] as the executable path */
  201.         int argc;
  202.         char **argv;
  203.         Py_GetArgcArgv(&argc, &argv);
  204.         if(argc>0)
  205.             strcpy(path,argv[0]);
  206.         else
  207.             strcpy(path,"!error!");
  208.     }
  209.     else
  210.     {
  211.         /* Launced from CLI, use GetProgramName */
  212.  
  213.         /* However, first check if the specified name exists */
  214.         if(!isxfile(path))
  215.         {
  216.             path[0]='\0';
  217.             return path;
  218.         }
  219.  
  220.         path[0]=0;
  221.         if(dir=GetProgramDir())
  222.         {
  223.             (void)NameFromLock(dir,path,MAXPATHLEN);
  224.             if(!GetProgramName(prog,MAXPATHLEN))    // this is a dos.library function!
  225.                 strcpy(prog,"!error!");
  226.             if(!AddPart(path,prog,MAXPATHLEN*2))
  227.                 strcpy(path,"!error!");
  228.         }    
  229.     }
  230.     return path;
  231. }
  232.  
  233. static void reduce( char *dir)
  234. {
  235.     int i = strlen(dir);
  236.     if(i>0 && dir[i-1]==':') dir[--i]=0;
  237.     while (i > 0 && dir[i] != SEP && dir[i] != ':') --i;
  238.     if(dir[i]!=':') dir[i] = '\0';
  239.     else dir[i+1]='\0';
  240. }
  241.  
  242. #else /*! AMIGA */
  243.  
  244. static void
  245. reduce(dir)
  246.     char *dir;
  247. {
  248.     int i = strlen(dir);
  249.     while (i > 0 && dir[i] != SEP)
  250.         --i;
  251.     dir[i] = '\0';
  252. }
  253.  
  254. #endif
  255.  
  256. #ifndef S_ISREG
  257. #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
  258. #endif
  259.  
  260. #ifndef S_ISDIR
  261. #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
  262. #endif
  263.  
  264. static int
  265. isfile(filename)        /* Is file, not directory */
  266.     char *filename;
  267. {
  268.     struct stat buf;
  269.     if (stat(filename, &buf) != 0)
  270.         return 0;
  271.     if (!S_ISREG(buf.st_mode))
  272.         return 0;
  273.     return 1;
  274. }
  275.  
  276.  
  277. static int
  278. ismodule(filename)        /* Is module -- check for .pyc/.pyo too */
  279.     char *filename;
  280. {
  281.     if (isfile(filename))
  282.         return 1;
  283.  
  284.     /* Check for the compiled version of prefix. */
  285.     if (strlen(filename) < MAXPATHLEN) {
  286.         strcat(filename, Py_OptimizeFlag ? "o" : "c");
  287.         if (isfile(filename))
  288.             return 1;
  289.     }
  290.     return 0;
  291. }
  292.  
  293.  
  294. static int
  295. isxfile(filename)        /* Is executable file */
  296.     char *filename;
  297. {
  298.     struct stat buf;
  299.     if (stat(filename, &buf) != 0)
  300.         return 0;
  301.     if (!S_ISREG(buf.st_mode))
  302.         return 0;
  303.     if ((buf.st_mode & 0111) == 0)
  304.         return 0;
  305.     return 1;
  306. }
  307.  
  308.  
  309. static int
  310. isdir(filename)            /* Is directory */
  311.     char *filename;
  312. {
  313.     struct stat buf;
  314.     if (stat(filename, &buf) != 0)
  315.         return 0;
  316.     if (!S_ISDIR(buf.st_mode))
  317.         return 0;
  318.     return 1;
  319. }
  320.  
  321.  
  322. #ifdef _AMIGA
  323. #define joinpath(buffer,stuff) AddPart(buffer,stuff,MAXPATHLEN)
  324. #else
  325. static void
  326. joinpath(buffer, stuff)
  327.     char *buffer;
  328.     char *stuff;
  329. {
  330.     int n, k;
  331.     if (stuff[0] == SEP)
  332.         n = 0;
  333.     else {
  334.         n = strlen(buffer);
  335.         if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
  336.             buffer[n++] = SEP;
  337.     }
  338.     k = strlen(stuff);
  339.     if (n + k > MAXPATHLEN)
  340.         k = MAXPATHLEN - n;
  341.     strncpy(buffer+n, stuff, k);
  342.     buffer[n+k] = '\0';
  343. }
  344. #endif
  345.  
  346. static int
  347. search_for_prefix(argv0_path, home)
  348.     char *argv0_path;
  349.     char *home;
  350. {
  351.     int n;
  352.     char *vpath;
  353.  
  354.     /* Check to see if argv[0] is in the build directory */
  355.     strcpy(prefix, argv0_path);
  356.     joinpath(prefix, "Modules/Setup");
  357.     if (isfile(prefix)) {
  358.         /* Check VPATH to see if argv0_path is in the build directory.
  359.          * Complication: the VPATH passed in is relative to the
  360.          * Modules build directory and points to the Modules source
  361.          * directory; we need it relative to the build tree and
  362.          * pointing to the source tree.  Solution: chop off a leading
  363.          * ".." (but only if it's there -- it could be an absolute
  364.          * path) and chop off the final component (assuming it's
  365.          * "Modules").
  366.          */
  367.         vpath = VPATH;
  368.         if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
  369.             vpath += 3;
  370.         strcpy(prefix, argv0_path);
  371.         joinpath(prefix, vpath);
  372.         reduce(prefix);
  373.         joinpath(prefix, "Lib");
  374.         joinpath(prefix, LANDMARK);
  375.         if (ismodule(prefix))
  376.             return -1;
  377.     }
  378.  
  379.     if (home) {
  380.         /* Check $PYTHONHOME */
  381.         char *delim;
  382.         strcpy(prefix, home);
  383.         delim = strchr(prefix, DELIM);
  384.         if (delim)
  385.             *delim = '\0';
  386.         joinpath(prefix, lib_python);
  387.         joinpath(prefix, LANDMARK);
  388.         if (ismodule(prefix))
  389.             return 1;
  390.     }
  391.  
  392.     /* Search from argv0_path, until root is found */
  393.     strcpy(prefix, argv0_path);
  394.     do {
  395.         n = strlen(prefix);
  396.         joinpath(prefix, lib_python);
  397.         joinpath(prefix, LANDMARK);
  398.         if (ismodule(prefix))
  399.             return 1;
  400.         prefix[n] = '\0';
  401.         reduce(prefix);
  402.     } while (prefix[0]);
  403.  
  404.     /* Look at configure's PREFIX */
  405.     strcpy(prefix, PREFIX);
  406.     joinpath(prefix, lib_python);
  407.     joinpath(prefix, LANDMARK);
  408.     if (ismodule(prefix))
  409.         return 1;
  410.  
  411.     /* Fail */
  412.     return 0;
  413. }
  414.  
  415.  
  416. static int
  417. search_for_exec_prefix(argv0_path, home)
  418.     char *argv0_path;
  419.     char *home;
  420. {
  421.     int n;
  422.  
  423.     /* Check to see if argv[0] is in the build directory */
  424.     strcpy(exec_prefix, argv0_path);
  425.     joinpath(exec_prefix, "Modules/Setup");
  426.     if (isfile(exec_prefix)) {
  427.         reduce(exec_prefix);
  428.         return -1;
  429.     }
  430.  
  431.     if (home) {
  432.         /* Check $PYTHONHOME */
  433.         char *delim;
  434.         delim = strchr(home, DELIM);
  435.         if (delim)
  436.             strcpy(exec_prefix, delim+1);
  437.         else
  438.             strcpy(exec_prefix, home);
  439.         joinpath(exec_prefix, lib_python);
  440.         joinpath(exec_prefix, "lib-dynload");
  441.         if (isdir(exec_prefix))
  442.             return 1;
  443.     }
  444.  
  445.     /* Search from argv0_path, until root is found */
  446.     strcpy(exec_prefix, argv0_path);
  447.     do {
  448.         n = strlen(exec_prefix);
  449.         joinpath(exec_prefix, lib_python);
  450.         joinpath(exec_prefix, "lib-dynload");
  451.         if (isdir(exec_prefix))
  452.             return 1;
  453.         exec_prefix[n] = '\0';
  454.         reduce(exec_prefix);
  455.     } while (exec_prefix[0]);
  456.  
  457.     /* Look at configure's EXEC_PREFIX */
  458.     strcpy(exec_prefix, EXEC_PREFIX);
  459.     joinpath(exec_prefix, lib_python);
  460.     joinpath(exec_prefix, "lib-dynload");
  461.     if (isdir(exec_prefix))
  462.         return 1;
  463.  
  464.     /* Fail */
  465.     return 0;
  466. }
  467.  
  468.  
  469. static void
  470. calculate_path()
  471. {
  472.     extern char *Py_GetProgramName();
  473.  
  474.     static char delimiter[2] = {DELIM, '\0'};
  475.     static char separator[2] = {SEP, '\0'};
  476.     char *pythonpath = PYTHONPATH;
  477.     char *rtpypath = getenv("PYTHONPATH");
  478.     char *home = Py_GetPythonHome();
  479. #ifndef _AMIGA
  480.     char *path = getenv("PATH");
  481.     char *prog = Py_GetProgramName();
  482. #endif
  483.     char argv0_path[MAXPATHLEN+1];
  484.     int pfound, efound; /* 1 if found; -1 if found build directory */
  485.     char *buf;
  486.     int bufsz;
  487.     int prefixsz;
  488.     char *defpath = pythonpath;
  489.  
  490.     /* Initialize this dynamically for K&R C */
  491.     sprintf(lib_python, "lib/python%s", VERSION);
  492.  
  493. #ifdef _AMIGA
  494.     strcpy(progpath,fullprogpath());
  495. #else /* !AMIGA */
  496.     /* If there is no slash in the argv0 path, then we have to
  497.      * assume python is on the user's $PATH, since there's no
  498.      * other way to find a directory to start the search from.  If
  499.      * $PATH isn't exported, you lose.
  500.      */
  501.     if (strchr(prog, SEP))
  502.         strcpy(progpath, prog);
  503.     else if (path) {
  504.         while (1) {
  505.             char *delim = strchr(path, DELIM);
  506.  
  507.             if (delim) {
  508.                 int len = delim - path;
  509.                 strncpy(progpath, path, len);
  510.                 *(progpath + len) = '\0';
  511.             }
  512.             else
  513.                 strcpy(progpath, path);
  514.  
  515.             joinpath(progpath, prog);
  516.             if (isxfile(progpath))
  517.                 break;
  518.  
  519.             if (!delim) {
  520.                 progpath[0] = '\0';
  521.                 break;
  522.             }
  523.             path = delim + 1;
  524.         }
  525.     }
  526.     else
  527.         progpath[0] = '\0';
  528. #endif
  529.  
  530.     strcpy(argv0_path, progpath);
  531.     
  532. #ifdef HAVE_READLINK
  533.     {
  534.         char tmpbuffer[MAXPATHLEN+1];
  535.         int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
  536.         if (linklen != -1) {
  537.             /* It's not null terminated! */
  538.             tmpbuffer[linklen] = '\0';
  539. #ifdef _AMIGA
  540.             if (NULL==strchr(tmpbuffer,':'))
  541. #else
  542.             if (tmpbuffer[0] == SEP)
  543. #endif
  544.                 strcpy(argv0_path, tmpbuffer);
  545.             else {
  546.                 /* Interpret relative to progpath */
  547.                 reduce(argv0_path);
  548.                 joinpath(argv0_path, tmpbuffer);
  549.             }
  550.         }
  551.     }
  552. #endif /* HAVE_READLINK */
  553.  
  554.     reduce(argv0_path);
  555.  
  556.     if (!(pfound = search_for_prefix(argv0_path, home))) {
  557.         if (!Py_FrozenFlag)
  558.         fprintf(stderr,
  559.            "Could not find platform independent libraries <prefix>\n");
  560.         strcpy(prefix, PREFIX);
  561.         joinpath(prefix, lib_python);
  562.     }
  563.     else
  564.         reduce(prefix);
  565.     
  566.     if (!(efound = search_for_exec_prefix(argv0_path, home))) {
  567.         if (!Py_FrozenFlag)
  568.         fprintf(stderr,
  569.         "Could not find platform dependent libraries <exec_prefix>\n");
  570.         strcpy(exec_prefix, EXEC_PREFIX);
  571.         joinpath(exec_prefix, "lib/lib-dynload");
  572.     }
  573.     /* If we found EXEC_PREFIX do *not* reduce it!  (Yet.) */
  574.  
  575.     if ((!pfound || !efound) && !Py_FrozenFlag)
  576.         fprintf(stderr,
  577.          "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
  578.  
  579.     /* Calculate size of return buffer.
  580.      */
  581.     bufsz = 0;
  582.  
  583.     if (rtpypath)
  584.         bufsz += strlen(rtpypath) + 1;
  585.  
  586.     prefixsz = strlen(prefix) + 1;
  587.  
  588.     while (1) {
  589.         char *delim = strchr(defpath, DELIM);
  590.  
  591. #ifdef _AMIGA
  592.         if (NULL==strchr(defpath,':'))
  593. #else
  594.         if (defpath[0] != SEP)
  595. #endif
  596.             /* Paths are relative to prefix */
  597.             bufsz += prefixsz;
  598.  
  599.         if (delim)
  600.             bufsz += delim - defpath + 1;
  601.         else {
  602.             bufsz += strlen(defpath) + 1;
  603.             break;
  604.         }
  605.         defpath = delim + 1;
  606.     }
  607.  
  608.     bufsz += strlen(exec_prefix) + 1;
  609.  
  610.     /* This is the only malloc call in this file */
  611.     buf = malloc(bufsz);
  612.  
  613.     if (buf == NULL) {
  614.         /* We can't exit, so print a warning and limp along */
  615.         fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
  616.         fprintf(stderr, "Using default static PYTHONPATH.\n");
  617.         module_search_path = PYTHONPATH;
  618.     }
  619.     else {
  620.         /* Run-time value of $PYTHONPATH goes first */
  621.         if (rtpypath) {
  622.             strcpy(buf, rtpypath);
  623.             strcat(buf, delimiter);
  624.         }
  625.         else
  626.             buf[0] = '\0';
  627.  
  628.         /* Next goes merge of compile-time $PYTHONPATH with
  629.          * dynamically located prefix.
  630.          */
  631.         defpath = pythonpath;
  632.         while (1) {
  633.             char *delim = strchr(defpath, DELIM);
  634.  
  635. #ifdef _AMIGA
  636.             if (NULL==strchr(defpath,':')) {
  637. #else
  638.             if (defpath[0] != SEP) {
  639. #endif
  640.                 strcat(buf, prefix);
  641.                 strcat(buf, separator);
  642.             }
  643.  
  644.             if (delim) {
  645.                 int len = delim - defpath + 1;
  646.                 int end = strlen(buf) + len;
  647.                 strncat(buf, defpath, len);
  648.                 *(buf + end) = '\0';
  649.             }
  650.             else {
  651.                 strcat(buf, defpath);
  652.                 break;
  653.             }
  654.             defpath = delim + 1;
  655.         }
  656.         strcat(buf, delimiter);
  657.  
  658.         /* Finally, on goes the directory for dynamic-load modules */
  659.         strcat(buf, exec_prefix);
  660.  
  661.         /* And publish the results */
  662.         module_search_path = buf;
  663.     }
  664.  
  665.     /* Reduce prefix and exec_prefix to their essence,
  666.      * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
  667.      * If we're loading relative to the build directory,
  668.      * return the compiled-in defaults instead.
  669.      */
  670.     if (pfound > 0) {
  671.         reduce(prefix);
  672.         reduce(prefix);
  673.     }
  674.     else
  675.         strcpy(prefix, PREFIX);
  676.  
  677.     if (efound > 0) {
  678.         reduce(exec_prefix);
  679.         reduce(exec_prefix);
  680.         reduce(exec_prefix);
  681.     }
  682.     else
  683.         strcpy(exec_prefix, EXEC_PREFIX);
  684. }
  685.  
  686.  
  687. /* External interface */
  688.  
  689. char *
  690. Py_GetPath()
  691. {
  692.     if (!module_search_path)
  693.         calculate_path();
  694.     return module_search_path;
  695. }
  696.  
  697. char *
  698. Py_GetPrefix()
  699. {
  700.     if (!module_search_path)
  701.         calculate_path();
  702.     return prefix;
  703. }
  704.  
  705. char *
  706. Py_GetExecPrefix()
  707. {
  708.     if (!module_search_path)
  709.         calculate_path();
  710.     return exec_prefix;
  711. }
  712.  
  713. char *
  714. Py_GetProgramFullPath()
  715. {
  716.     if (!module_search_path)
  717.         calculate_path();
  718.     return progpath;
  719. }
  720.